package com.zx.platform.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import com.zx.map.service.model.AgentInfoModel;
import com.zx.map.utils.SUConstants;
import com.zx.platform.utils.StrUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Component;

import com.zx.platform.dao.RolesDao;
import com.zx.platform.dao.model.RoleMenusTemp;
import com.zx.platform.dao.model.Roles;
import com.zx.platform.dao.model.RolesExample;
import com.zx.platform.service.RolesService;
import com.zx.platform.service.domain.RolesDomain;
import com.zx.platform.service.model.RoleMenusModel;
import com.zx.platform.service.model.RolesModel;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 14-3-1 Time: 上午11:07 To
 * change this template use File | Settings | File Templates.
 */
@Component("rolesService")
public class RolesServiceImpl implements RolesService {

	@Autowired
	private RolesDao dao;

	/**
	 * 得到所有数据列表
	 */
	public List getAllList() throws DataAccessException {

		List<Roles> list = dao.getListByExample(null);
		List<RolesModel> modelList = new ArrayList<RolesModel>();
		for (Roles o : list) {
			RolesModel temp = new RolesModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;

	}

	/**
	 * 根据单Key查找Model
	 */
	public RolesModel getModelByKey(Integer key) throws DataAccessException {
		Roles o = dao.selectByPrimaryKey(key);
		RolesModel model = new RolesModel();
		BeanUtils.copyProperties(o, model);
		return model;
	}

	/**
	 * 根据Model查找列表
	 */
	public List getListByModel(RolesModel model) throws DataAccessException {
		RolesExample example = new RolesExample();
		com.zx.platform.dao.model.RolesExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(model.getRoleName())) {
			criteria.andRoleNameLike('%' + model.getRoleName() + '%');
		}
		List<Roles> list = dao.getListByExample(example);
		List<RolesModel> modelList = new ArrayList<RolesModel>();
		for (Roles o : list) {
			RolesModel temp = new RolesModel();
			BeanUtils.copyProperties(o, temp);
			modelList.add(temp);
		}
		return modelList;
	}

	/**
	 * 新增
	 */
	public Integer add(RolesModel model) throws DataAccessException {
		Roles temp = new Roles();
		BeanUtils.copyProperties(model, temp);
		Integer key = dao.add(temp);
		return key;
	}

	/**
	 * 修改
	 */
	public Integer update(RolesModel model) throws DataAccessException {
		Roles temp = new Roles();
		BeanUtils.copyProperties(model, temp);
		dao.update(temp);
		return model.getRoleId();
	}

	/**
	 * 删除
	 */
	public void delete(RolesModel model) throws DataAccessException {
		Roles temp = new Roles();
		BeanUtils.copyProperties(model, temp);
		dao.delete(temp);
	}

	/**
	 * 分页查询
	 *
	 * @param pageSize
	 * @param pageNum
	 * @return
	 * @throws DataAccessException
	 */
	public Map getPageByDomain(int pageSize, int pageNum, RolesDomain domain)
			throws DataAccessException {

		Map<String, Object> map = new HashMap<String, Object>();
		RolesExample example = new RolesExample();
		com.zx.platform.dao.model.RolesExample.Criteria criteria = example.createCriteria();
		if (StringUtils.isNotBlank(domain.getRoleName())) {
			criteria.andRoleNameLike('%' + domain.getRoleName() + '%');
		}
		//排序add by wenjw 2015.6.2
		example.setOrderByClause("create_Date desc");
		int pageBegin = (pageNum - 1) * pageSize;
		List<Roles> list = dao.getPageByExample(pageSize, pageBegin, example);
		List<RolesModel> menuModelList = new ArrayList<RolesModel>();
		for (Roles o : list) {
			RolesModel temp = new RolesModel();
			BeanUtils.copyProperties(o, temp);
			menuModelList.add(temp);
		}
		int count = dao.countByExample(example);
		map.put("count", count);
		map.put("list", menuModelList);
		return map;
	}

	/**
	 * 设置角色菜单关联
	 * 是先删除再新增
	 */
	public Integer setRoleMenus(RoleMenusModel model) throws DataAccessException {
		RoleMenusTemp roleMenusTemp = new RoleMenusTemp();
		BeanUtils.copyProperties(model, roleMenusTemp);
		return dao.setRoleMenus(roleMenusTemp);
	}

	@Override
	public List<Roles> getDownAgentRoles(AgentInfoModel currentLoginAgentInfo) {
		RolesExample example = new RolesExample();
		RolesExample.Criteria criteria = example.createCriteria();
		int currentLevel = Integer.valueOf(SUConstants.AGENT_LEVEL_HEAD);
		int lastLevel = Integer.valueOf(SUConstants.AGENT_LEVEL_COMMON);
		if (currentLoginAgentInfo == null) {
		} else {
			String agentLevel = currentLoginAgentInfo.getAgentLevel();
			currentLevel = Integer.valueOf(agentLevel);
		}
		criteria.andRoleIdBetween(currentLevel+1,lastLevel);
		example.setOrderByClause("role_id asc");
		List<Roles>roleList=dao.getListByExample(example);
		return roleList;
	}

	@Override
	public List<Roles> getDownAgentRolesWithWillAgent(AgentInfoModel currentLoginAgentInfo) {
		RolesExample example = new RolesExample();
		RolesExample.Criteria criteria = example.createCriteria();
		int currentLevel = Integer.valueOf(SUConstants.AGENT_LEVEL_HEAD);
		int lastLevel = Integer.valueOf(SUConstants.AGENT_LEVEL_WILL);
		if (currentLoginAgentInfo == null) {
		} else {
			String agentLevel = currentLoginAgentInfo.getAgentLevel();
			currentLevel = Integer.valueOf(agentLevel);
		}
		criteria.andRoleIdBetween(currentLevel+1,lastLevel);
		example.setOrderByClause("role_id asc");
		List<Roles>roleList=dao.getListByExample(example);
		return roleList;
	}
	@Override
	public Roles getLevelNameByRoleId(String id) {
		Roles roles = new Roles();
		if (StrUtils.isNotBlank(id)) {
			roles = dao.selectByPrimaryKey(Integer.parseInt(id)) ;
		}
		return roles;
	}

}
